package com.zs.toolbox.web2j.system.storage.controller;

import com.zs.toolbox.web2j.common.exception.WtExceptions;
import com.zs.toolbox.web2j.common.response.WtBaseResponse;
import com.zs.toolbox.web2j.common.web.WtBaseController;
import com.zs.toolbox.web2j.system.message.domain.response.WtUploadFileResponse;
import com.zs.toolbox.web2j.system.storage.service.WtStorageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @author shuai.zhang  on 2018/9/22
 * @description
 */
@RestController
@RequestMapping("/api/v1/storage/")
@Slf4j
@Api("文件上传接口")
public class WtStorageController extends WtBaseController {


    @Value("${wt.storage.visit.url.perfix}")
    String cdnUrlPrefix;


    @Autowired
    WtStorageService wtStorageService;

    @RequestMapping(value = "upload", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @ApiOperation(value = "单文件上传接口")
    public WtBaseResponse uploadFile(@RequestPart(value = "fileType", required = false) String fileType, MultipartFile file) {
        String relativePath = _uploadFile(fileType, file);
        return new WtBaseResponse(new WtUploadFileResponse(relativePath, String.format("%s/%s", cdnUrlPrefix, relativePath)));
    }

    private String _uploadFile(String fileType, MultipartFile file) {
        if (StringUtils.isBlank(fileType)) {
            fileType = "default";
        }
        String extension = FilenameUtils.getExtension(file.getOriginalFilename());
        String saveFileName = String.format("%s/%s_%s.%s", fileType, System.currentTimeMillis(), RandomStringUtils.randomNumeric(8), extension);
        try {
            wtStorageService.saveFile(saveFileName, file.getInputStream());
        } catch (IOException e) {
            log.warn(String.format("upload file[%s] error", file.getOriginalFilename()), e);
            throw WtExceptions.E_STORAGE_FTP_UPLOAD;
        }
        return saveFileName;
    }

/*    @RequestMapping(value = "uploads", method = RequestMethod.POST,consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @ApiOperation(value = "多文件上传接口")
    @ResponseBody
    public WtBaseResponse uploadFiles(@RequestPart(value = "fileType",required = false) String fileType, MultipartFile[] file) {
        List<WtUploadFileResponse>  list = new ArrayList<>();

        for (MultipartFile multipartFile : file) {
            String relativePath = _uploadFile(fileType, multipartFile);
            WtUploadFileResponse wtUploadFileResponse = new WtUploadFileResponse(relativePath, String.format("%s/%s", cdnUrlPrefix, relativePath));
            list.add(wtUploadFileResponse);
        }
        return new WtBaseResponse(list);
    }*/

}
